package com.changgou.business.service.impl;

import com.changgou.business.dao.ActivityMapper;
import com.changgou.business.pojo.Activity;
import com.changgou.business.service.ActivityService;
import com.changgou.entity.PageResult;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class ActivityServiceImpl implements ActivityService {

    @Autowired
    private ActivityMapper activityMapper;
    /**
     * 添加活动
     * @param activity
     */
    @Override
    public void add(Activity activity) {
        activityMapper.insertSelective(activity);
    }

    /**
     * 分页条件查询活动列表
     * @param page
     * @param size
     * @param condition
     * @return
     */
    @Override
    public PageResult<Activity> findByPage(int page, int size, String condition) {
        //设置分页
        PageHelper.startPage(page,size);
        //创建Example对象
        Example example = new Example(Activity.class);
        //创建criteria对象
        Example.Criteria criteria = example.createCriteria();
        //拼接条件
        criteria.andLike("name","%"+condition+"%");
        //查询
        Page<Activity> activities = (Page<Activity>) activityMapper.selectByExample(example);
        //创建PageResult对象
        PageResult<Activity> pageInfo = new PageResult<>(activities.getTotal(),activities.getResult());
        return pageInfo;
    }

    /**
     * 上下架活动
     * @param id
     */
    @Override
    public void updateMarketable(Integer id) {
        //先查询上下架
        Activity activity = activityMapper.selectByPrimaryKey(id);
        //获取上下架状态
        Integer ismarketable = activity.getIsmarketable();
        //创建Activity对象
        Activity activity1 = new Activity();
        activity1.setId(id);
        if(ismarketable == 1){
            activity1.setIsmarketable(0);
            activityMapper.updateByPrimaryKeySelective(activity1);
        }else{
            activity1.setIsmarketable(1);
            activityMapper.updateByPrimaryKeySelective(activity1);
        }
    }

    /**
     * 删除活动
     * @param id
     */
    @Override
    public void deleteById(Integer id) {
        activityMapper.deleteByPrimaryKey(id);
    }
}
